#!/usr/bin/perl
# 
#  Exercise 7.3 
# 
# Write a subroutine to randomly shuffle the elements of an array. The subroutine 
# should take an array as an argument and return an array with the same elements but 
# shuffled in a random order. Each element of the original array should appear 
# exactly once in the output array, just like shuffling a deck of cards.
#
# Answer to Exercise 7.3

use strict;
use warnings;

srand(time|$$);

my @array = qw(0 1 2 3 4 5 6 7 8 9);

my @shuffled_array = shuffle( @array );

print "@shuffled_array\n";

exit;

sub shuffle {

	my(@array) = @_;

	my(@shuffle) = ();

	while(@array) {

		my $offset = int rand @array;

		my $element = $array[$offset];

		push(@shuffle, $element);

		splice(@array, $offset, 1);
	}

	return @shuffle;
}
